package org.xyl.game.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.xyl.game.entity.Userinfo;
import org.xyl.game.service.UserInfoService;
import org.xyl.game.tool.GsonUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Controller
public class LoginController {
    @Autowired
    private UserInfoService userInfoService;

    @PostMapping(path = "/login", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String login(String username, String password) {
        Userinfo userInfo = userInfoService.findUserInfo(username);
        Map<String, String> returnMap = new HashMap<>();
        if (userInfo == null) {
            returnMap.put("state", "未找到用户");
            return GsonUtil.getGsonBuilder().create().toJson(returnMap);
        }
        if (Objects.equals(userInfo.getPassword(), password)) {

            returnMap.put("username", userInfo.getUsername());
            returnMap.put("userid", userInfo.getUserid() + "");
            return GsonUtil.getGsonBuilder().create().toJson(returnMap);
        }
        returnMap.put("state", "密码错误");
        return GsonUtil.getGsonBuilder().create().toJson(returnMap);


    }
}
